home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / paragraphs.el.z / paragraphs.el
Encoding:
Text File  |  1998-05-21  |  15.2 KB  |  404 lines

  1. ;;; paragraphs.el --- paragraph and sentence parsing.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 97 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: wp
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; This package provides the paragraph-oriented commands documented in the
  30. ;; XEmacs Reference Manual.
  31.  
  32. ;; 06/11/1997 - Use char-(after|before) instead of
  33. ;;  (following|preceding)-char. -slb
  34.  
  35. ;;; Code:
  36.  
  37. (defvar use-hard-newlines nil
  38.     "Non-nil means to distinguish hard and soft newlines.
  39. When this is non-nil, the functions `newline' and `open-line' add the
  40. text-property `hard' to newlines that they insert.  Also, a line is
  41. only considered as a candidate to match `paragraph-start' or
  42. `paragraph-separate' if it follows a hard newline.  Newlines not
  43. marked hard are called \"soft\", and are always internal to
  44. paragraphs.  The fill functions always insert soft newlines.
  45.  
  46. Each buffer has its own value of this variable.")
  47. (make-variable-buffer-local 'use-hard-newlines)
  48.  
  49. (defun use-hard-newlines (&optional arg insert)
  50.   "Minor mode to distinguish hard and soft newlines.
  51. When active, the functions `newline' and `open-line' add the
  52. text-property `hard' to newlines that they insert, and a line is
  53. only considered as a candidate to match `paragraph-start' or
  54. `paragraph-separate' if it follows a hard newline.
  55.  
  56. Prefix argument says to turn mode on if positive, off if negative.
  57. When the mode is turned on, if there are newlines in the buffer but no hard
  58. newlines, ask the user whether to mark as hard any newlines preceeding a 
  59. `paragraph-start' line.  From a program, second arg INSERT specifies whether
  60. to do this; it can be `never' to change nothing, t or `always' to force
  61. marking, `guess' to try to do the right thing with no questions, nil 
  62. or anything else to ask the user.
  63.  
  64. Newlines not marked hard are called \"soft\", and are always internal
  65. to paragraphs.  The fill functions insert and delete only soft newlines."
  66.   (interactive (list current-prefix-arg nil))
  67.   (if (or (<= (prefix-numeric-value arg) 0)
  68.       (and use-hard-newlines (null arg)))
  69.       ;; Turn mode off
  70.       (setq use-hard-newlines nil)
  71.     ;; Turn mode on
  72.     ;; Intuit hard newlines --
  73.     ;;   mark as hard any newlines preceding a paragraph-start line.
  74.     (if (or (eq insert t) (eq insert 'always)
  75.         (and (not (eq 'never insert))
  76.          (not use-hard-newlines)
  77.          (not (text-property-any (point-min) (point-max) 'hard t))
  78.          (save-excursion
  79.            (goto-char (point-min))
  80.            (search-forward "\n" nil t))
  81.          (or (eq insert 'guess)
  82.              (y-or-n-p "Make newlines between paragraphs hard? "))))
  83.     (save-excursion
  84.       (goto-char (point-min))
  85.       (while (search-forward "\n" nil t)
  86.         (let ((pos (point)))
  87.           (move-to-left-margin)
  88.           (if (looking-at paragraph-start)
  89.           (progn
  90.             (set-hard-newline-properties (1- pos) pos)
  91.             ;; If paragraph-separate, newline after it is hard too.
  92.             (if (looking-at paragraph-separate)
  93.             (progn
  94.               (end-of-line)
  95.               (if (not (eobp))
  96.                   (set-hard-newline-properties
  97.                    (point) (1+ (point))))))))))))
  98.     (setq use-hard-newlines t)))
  99.  
  100. ;; XEmacs - use purecopy
  101. (defconst paragraph-start (purecopy "[ \t\n\f]") "\
  102. *Regexp for beginning of a line that starts OR separates paragraphs.
  103. This regexp should match lines that separate paragraphs
  104. and should also match lines that start a paragraph
  105. \(and are part of that paragraph).
  106.  
  107. This is matched against the text at the left margin, which is not necessarily
  108. the beginning of the line, so it should never use \"^\" as an anchor.  This
  109. ensures that the paragraph functions will work equally well within a region
  110. of text indented by a margin setting.
  111.  
  112. The variable `paragraph-separate' specifies how to distinguish
  113. lines that start paragraphs from lines that separate them.
  114.  
  115. If the variable `use-hard-newlines' is non-nil, then only lines following a
  116. hard newline are considered to match.")
  117.  
  118. ;; paragraph-start requires a hard newline, but paragraph-separate does not:
  119. ;; It is assumed that paragraph-separate is distinctive enough to be believed
  120. ;; whenever it occurs, while it is reasonable to set paragraph-start to
  121. ;; something very minimal, even including "." (which makes every hard newline
  122. ;; start a new paragraph).
  123.  
  124. ;; XEmacs -- use purecopy
  125. (defconst paragraph-separate (purecopy "[ \t\f]*$") "\
  126. *Regexp for beginning of a line that separates paragraphs.
  127. If you change this, you may have to change paragraph-start also.
  128.  
  129. This is matched against the text at the left margin, which is not necessarily
  130. the beginning of the line, so it should not use \"^\" as an anchor.  This
  131. ensures that the paragraph functions will work equally within a region of
  132. text indented by a margin setting.")
  133.  
  134. ;; XEmacs -- use purecopy
  135. (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*") "\
  136. *Regexp describing the end of a sentence.
  137. All paragraph boundaries also end sentences, regardless.
  138.  
  139. In order to be recognized as the end of a sentence, the ending period,
  140. question mark, or exclamation point must be followed by two spaces,
  141. unless it's inside some sort of quotes or parenthesis.")
  142.  
  143. ;; XEmacs -- use purecopy
  144. (defconst page-delimiter (purecopy "^\014") "\
  145. *Regexp describing line-beginnings that separate pages.")
  146.  
  147. (defvar paragraph-ignore-fill-prefix nil "\
  148. Non-nil means the paragraph commands are not affected by `fill-prefix'.
  149. This is desirable in modes where blank lines are the paragraph delimiters.")
  150.  
  151. (defun forward-paragraph (&optional arg)
  152.   "Move forward to end of paragraph.
  153. With arg N, do it N times; negative arg -N means move backward N paragraphs.
  154.  
  155. A line which `paragraph-start' matches either separates paragraphs
  156. \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
  157. A paragraph end is the beginning of a line which is not part of the paragraph
  158. to which the end of the previous line belongs, or the end of the buffer."
  159.   (interactive "_p") ; XEmacs
  160.   (or arg (setq arg 1))
  161.   (let* ((fill-prefix-regexp
  162.       (and fill-prefix (not (equal fill-prefix ""))
  163.            (not paragraph-ignore-fill-prefix)
  164.            (regexp-quote fill-prefix)))
  165.      ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
  166.      ;; These regexps shouldn't be anchored, because we look for them
  167.      ;; starting at the left-margin.  This allows paragraph commands to
  168.      ;; work normally with indented text.
  169.      ;; This hack will not find problem cases like "whatever\\|^something".
  170.      (paragraph-start (if (and (not (equal "" paragraph-start))
  171.                    (equal ?^ (aref paragraph-start 0)))
  172.                   (substring paragraph-start 1)
  173.                 paragraph-start))
  174.      (paragraph-separate (if (and (not (equal "" paragraph-start))
  175.                       (equal ?^ (aref paragraph-separate 0)))
  176.                   (substring paragraph-separate 1)
  177.                 paragraph-separate))
  178.      (paragraph-separate
  179.       (if fill-prefix-regexp
  180.           (concat paragraph-separate "\\|"
  181.               fill-prefix-regexp "[ \t]*$")
  182.         paragraph-separate))
  183.      ;; This is used for searching.
  184.      (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
  185.      start)
  186.     (while (and (< arg 0) (not (bobp)))
  187.       (if (and (not (looking-at paragraph-separate))
  188.            (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
  189.            (looking-at paragraph-separate))
  190.       nil
  191.     (setq start (point))
  192.     ;; Move back over paragraph-separating lines.
  193.     (forward-char -1) (beginning-of-line)
  194.     (while (and (not (bobp))
  195.             (progn (move-to-left-margin)
  196.                (looking-at paragraph-separate)))
  197.       (forward-line -1)) 
  198.     (if (bobp)
  199.         nil
  200.       ;; Go to end of the previous (non-separating) line.
  201.       (end-of-line)
  202.       ;; Search back for line that starts or separates paragraphs.
  203.       (if (if fill-prefix-regexp
  204.           ;; There is a fill prefix; it overrides paragraph-start.
  205.           (let (multiple-lines)
  206.             (while (and (progn (beginning-of-line) (not (bobp)))
  207.                 (progn (move-to-left-margin)
  208.                        (not (looking-at paragraph-separate)))
  209.                 (looking-at fill-prefix-regexp))
  210.               (if (not (= (point) start))
  211.               (setq multiple-lines t))
  212.               (forward-line -1))
  213.             (move-to-left-margin)
  214.             ;; Don't move back over a line before the paragraph
  215.             ;; which doesn't start with fill-prefix
  216.             ;; unless that is the only line we've moved over.
  217.             (and (not (looking-at fill-prefix-regexp))
  218.              multiple-lines
  219.              (forward-line 1))
  220.             (not (bobp)))
  221.         (while (and (re-search-backward sp-paragraph-start nil 1)
  222.                 ;; Found a candidate, but need to check if it is a
  223.                 ;; REAL paragraph-start.
  224.                 (not (bobp))
  225.                 (progn (setq start (point))
  226.                    (move-to-left-margin)
  227.                    (not (looking-at paragraph-separate)))
  228.                 (or (not (looking-at paragraph-start))
  229.                 (and use-hard-newlines
  230.                      (not (get-text-property (1- start)
  231.                                  'hard)))))
  232.           (goto-char start))
  233.         (> (point) (point-min)))
  234.           ;; Found one.
  235.           (progn
  236.         ;; Move forward over paragraph separators.
  237.         ;; We know this cannot reach the place we started
  238.         ;; because we know we moved back over a non-separator.
  239.         (while (and (not (eobp))
  240.                 (progn (move-to-left-margin)
  241.                    (looking-at paragraph-separate)))
  242.           (forward-line 1))
  243.         ;; If line before paragraph is just margin, back up to there.
  244.         (end-of-line 0)
  245.         (if (> (current-column) (current-left-margin))
  246.             (forward-char 1)
  247.           (skip-chars-backward " \t")
  248.           (if (not (bolp))
  249.               (forward-line 1))))
  250.         ;; No starter or separator line => use buffer beg.
  251.         (goto-char (point-min)))))
  252.       (setq arg (1+ arg)))
  253.     (while (and (> arg 0) (not (eobp)))
  254.       ;; Move forward over separator lines, and one more line.
  255.       (while (prog1 (and (not (eobp))
  256.              (progn (move-to-left-margin) (not (eobp)))
  257.              (looking-at paragraph-separate))
  258.            (forward-line 1)))
  259.       (if fill-prefix-regexp
  260.       ;; There is a fill prefix; it overrides paragraph-start.
  261.       (while (and (not (eobp))
  262.               (progn (move-to-left-margin) (not (eobp)))
  263.               (not (looking-at paragraph-separate))
  264.               (looking-at fill-prefix-regexp))
  265.         (forward-line 1))
  266.     (while (and (re-search-forward sp-paragraph-start nil 1)
  267.             (progn (setq start (match-beginning 0))
  268.                (goto-char start)
  269.                (not (eobp)))
  270.             (progn (move-to-left-margin)
  271.                (not (looking-at paragraph-separate)))
  272.             (or (not (looking-at paragraph-start))
  273.             (and use-hard-newlines
  274.                  (not (get-text-property (1- start) 'hard)))))
  275.       (forward-char 1))
  276.     (if (< (point) (point-max))
  277.         (goto-char start)))
  278.       (setq arg (1- arg)))))
  279.  
  280. (defun backward-paragraph (&optional arg)
  281.   "Move backward to start of paragraph.
  282. With arg N, do it N times; negative arg -N means move forward N paragraphs.
  283.  
  284. A paragraph start is the beginning of a line which is a
  285. `first-line-of-paragraph' or which is ordinary text and follows a
  286. paragraph-separating line; except: if the first real line of a
  287. paragraph is preceded by a blank line, the paragraph starts at that
  288. blank line.
  289.  
  290. See `forward-paragraph' for more information."
  291.   (interactive "_p") ; XEmacs
  292.   (or arg (setq arg 1))
  293.   (forward-paragraph (- arg)))
  294.  
  295. (defun mark-paragraph ()
  296.   "Put point at beginning of this paragraph, mark at end.
  297. The paragraph marked is the one that contains point or follows point."
  298.   (interactive)
  299.   (forward-paragraph 1)
  300.   (push-mark nil t t)
  301.   (backward-paragraph 1))
  302.  
  303. (defun kill-paragraph (arg)
  304.   "Kill forward to end of paragraph.
  305. With arg N, kill forward to Nth end of paragraph;
  306. negative arg -N means kill backward to Nth start of paragraph."
  307.   (interactive "*p") ; XEmacs
  308.   (kill-region (point) (progn (forward-paragraph arg) (point))))
  309.  
  310. (defun backward-kill-paragraph (arg)
  311.   "Kill back to start of paragraph.
  312. With arg N, kill back to Nth start of paragraph;
  313. negative arg -N means kill forward to Nth end of paragraph."
  314.   (interactive "*p") ; XEmacs
  315.   (kill-region (point) (progn (backward-paragraph arg) (point))))
  316.  
  317. (defun transpose-paragraphs (arg)
  318.   "Interchange this (or next) paragraph with previous one."
  319.   (interactive "*p")
  320.   (transpose-subr 'forward-paragraph arg))
  321.  
  322. (defun start-of-paragraph-text ()
  323.   (let ((opoint (point)) npoint)
  324.     (forward-paragraph -1)
  325.     (setq npoint (point))
  326.     (skip-chars-forward " \t\n")
  327.     ;; If the range of blank lines found spans the original start point,
  328.     ;; try again from the beginning of it.
  329.     ;; Must be careful to avoid infinite loop
  330.     ;; when following a single return at start of buffer.
  331.     (if (and (>= (point) opoint) (< npoint opoint))
  332.     (progn
  333.       (goto-char npoint)
  334.       (if (> npoint (point-min))
  335.           (start-of-paragraph-text))))))
  336.  
  337. (defun end-of-paragraph-text ()
  338.   (let ((opoint (point)))
  339.     (forward-paragraph 1)
  340.     (if (eq (char-before (point)) ?\n) (forward-char -1))
  341.     (if (<= (point) opoint)
  342.     (progn
  343.       (forward-char 1)
  344.       (if (< (point) (point-max))
  345.           (end-of-paragraph-text))))))
  346.  
  347. (defun forward-sentence (&optional arg)
  348.   "Move forward to next `sentence-end'.  With argument, repeat.
  349. With negative argument, move backward repeatedly to `sentence-beginning'.
  350.  
  351. The variable `sentence-end' is a regular expression that matches ends of
  352. sentences.  Also, every paragraph boundary terminates sentences as well."
  353.   (interactive "_p") ; XEmacs
  354.   (or arg (setq arg 1))
  355.   (while (< arg 0)
  356.     (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
  357.       (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
  358.       (goto-char (1- (match-end 0)))
  359.     (goto-char par-beg)))
  360.     (setq arg (1+ arg)))
  361.   (while (> arg 0)
  362.     (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
  363.       (if (re-search-forward sentence-end par-end t)
  364.       (skip-chars-backward " \t\n")
  365.     (goto-char par-end)))
  366.     (setq arg (1- arg))))
  367.  
  368. (defun backward-sentence (&optional arg)
  369.   "Move backward to start of sentence.  With arg, do it arg times.
  370. See `forward-sentence' for more information."
  371.   (interactive "_p") ; XEmacs
  372.   (or arg (setq arg 1))
  373.   (forward-sentence (- arg)))
  374.  
  375. (defun kill-sentence (&optional arg)
  376.   "Kill from point to end of sentence.
  377. With arg, repeat; negative arg -N means kill back to Nth start of sentence."
  378.   (interactive "*p") ; XEmacs
  379.   (kill-region (point) (progn (forward-sentence arg) (point))))
  380.  
  381. (defun backward-kill-sentence (&optional arg)
  382.   "Kill back from point to start of sentence.
  383. With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
  384.   (interactive "*p") ; XEmacs
  385.   (kill-region (point) (progn (backward-sentence arg) (point))))
  386.  
  387. (defun mark-end-of-sentence (arg)
  388.   "Put mark at end of sentence.  Arg works as in `forward-sentence'."
  389.   (interactive "p")
  390.   ;; FSF Version:
  391. ;  (push-mark
  392. ;   (save-excursion
  393. ;     (forward-sentence arg)
  394. ;     (point))
  395. ;   nil t))
  396.   (mark-something 'mark-end-of-sentence 'forward-sentence arg))
  397.  
  398. (defun transpose-sentences (arg)
  399.   "Interchange this (next) and previous sentence."
  400.   (interactive "*p")
  401.   (transpose-subr 'forward-sentence arg))
  402.  
  403. ;;; paragraphs.el ends here
  404.